package com.sdcqjy.property.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.sdcqjy.mylibrary.OtherUtils;
import com.sdcqjy.mylibrary.adapter.SuperRecyclerAdapter;
import com.sdcqjy.property.R;
import com.sdcqjy.property.mode.LawRegulationsMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 磁磁帅 on 2018/7/4.
 * 功能：
 */
public class LawRegulationsAdapter extends BaseExpandableListAdapter {
    public LawRegulationsAdapter(Context context, List<LawRegulationsMode> modeList) {
        this.context = context;
        this.modeList = modeList;
        inflater = LayoutInflater.from(context);
    }

    public int getGroupCount() {
        return modeList.size();
    }

    public int getChildrenCount(int groupPosition) {
        return modeList.get(groupPosition).getModeList().size();
    }

    public Object getGroup(int groupPosition) {
        return modeList.get(groupPosition);
    }

    public Object getChild(int groupPosition, int childPosition) {
        return modeList.get(groupPosition).getModeList().get(childPosition);
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition << 32 + childPosition;
    }

    public boolean hasStableIds() {
        return true;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_law_regulations_list, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        LawRegulationsMode lawRegulationsMode = modeList.get(groupPosition);
        viewHolder.setData(lawRegulationsMode);
        return convertView;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_law_regulations_list, null);
            viewHolder = new ChildViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }
        LawRegulationsMode lawRegulationsMode = modeList.get(groupPosition).getModeList().get(childPosition);
        viewHolder.setData(lawRegulationsMode);
        return convertView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private Context context;
    private List<LawRegulationsMode> modeList;
    private LayoutInflater inflater;
    private ViewHolder viewHolder;

    //    public LawRegulationsAdapter(Context context, List<LawRegulationsMode> modeList) {
//        super(context, modeList);
//    }
//
//    public int getLayoutId() {
//        return R.layout.item_law_regulations_list;
//    }
//
//    public SuperRecyclerAdapter.ViewHolder<LawRegulationsMode> getViewHolder(View view) {
//        return new ViewHolder(view);
//    }
//
    private OnAdapterClickListener onAdapterClickListener;

    public LawRegulationsAdapter setOnAdapterClickListener(OnAdapterClickListener onAdapterClickListener) {
        this.onAdapterClickListener = onAdapterClickListener;
        return this;
    }

    public class ViewHolder extends SuperRecyclerAdapter.ViewHolder<LawRegulationsMode> implements View.OnClickListener {
        @BindView(R.id.textTitle)
        TextView textTitle;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(this);
            view.findViewById(R.id.imgArr).setVisibility(View.GONE);
        }

        public void setData(LawRegulationsMode mode) {
            super.setData(mode);
            textTitle.setText(mode.title);
            setSelect();
        }

        public void setSelect() {
            if (mode.isSelect) textTitle.setTextColor(0xFF9B7562);
            else textTitle.setTextColor(0xFF333333);
        }

        public void onClick(View v) {
            mode.isOpen = !mode.isOpen;
            if (mode.isOpen) {
                mode.isSelect = true;
            } else mode.isSelect = false;
            if (viewHolder != null && viewHolder != this) {
                viewHolder.mode.isSelect = false;
                viewHolder.setSelect();
            }
            viewHolder = this;
            setSelect();
            if (onAdapterClickListener != null) onAdapterClickListener.onAdapterClicked(mode);
        }
    }

    public class ChildViewHolder implements View.OnClickListener {
        @BindView(R.id.textTitle)
        TextView textTitle;
        private LawRegulationsMode mode;

        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
            textTitle.setTextColor(0xFF3DA686);
            view.setOnClickListener(this);
            view.setPadding(
                    OtherUtils.dip2px(context, 20)
                    , 0
                    , view.getPaddingRight()
                    , 0);
        }

        public void setData(LawRegulationsMode mode) {
            this.mode = mode;
            textTitle.setText(mode.title);
        }

        public void onClick(View v) {
            if (onAdapterClickListener != null) onAdapterClickListener.onAdapterClicked2(mode);
        }
    }

    public interface OnAdapterClickListener {
        public void onAdapterClicked(LawRegulationsMode mode);

        public void onAdapterClicked2(LawRegulationsMode mode);
    }
}
